package org.iteam.xlz.core.service.base;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.security.sha.ShaUtil;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.base.PaymentMode;

public class PaymentService extends BaseService<PaymentMode>{
	static PaymentService instance;
	
	private PaymentService(){}
	
	public static PaymentService getInstance(){
		if(null==instance){
			instance=new PaymentService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from paymentmode where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<PaymentMode> query(long page,long rows){
		try {
			return super.query("select * from paymentmode where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<PaymentMode>();
		}
	}
	
	public boolean create(PaymentMode model) throws IOException{
		SqlBuilder sql=SqlBuilder.createInsert("paymentmode");
		sql.addField("pmid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("payservername",model.getPayservername());
		sql.addField("account",model.getAccount());
		sql.addField("pwd",ShaUtil.toSha256String(ShaUtil.toSha256String(model.getPwd())));
		sql.addField("requesturl",model.getRequesturl());
		sql.addField("requestmethod",model.getRequestmethod());
		sql.addField("callbackurl",model.getCallbackurl());
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(PaymentMode model) throws IOException{
		SqlBuilder sql=SqlBuilder.createUpdate("paymentmode");
		sql.addField("payservername",model.getPayservername());
		sql.addField("account",model.getAccount());
		sql.addField("pwd",ShaUtil.toSha256String(ShaUtil.toSha256String(model.getPwd())));
		sql.addField("requesturl",model.getRequesturl());
		sql.addField("requestmethod",model.getRequestmethod());
		sql.addField("callbackurl",model.getCallbackurl());
		sql.addWhere("pmid",model.getPmid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String pmid){
		try {
			return super.updates("delete from paymentmode where 1=1 and pmid=?",pmid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public PaymentMode find(String pmid){
		try {
			return super.show("select * from paymentmode where 1=1 and pmid=?",pmid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new PaymentMode();
		}
	}
	
}
